package com.recruit.wechat.controller;


import com.recruit.wechat.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestMapping;
import com.recruit.wechat.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;

import com.recruit.wechat.bean.WechatArea;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2020-08-24
 */
@Controller
@RequestMapping("/WeChatUser/wechatArea")
public class WechatAreaController extends BaseController{
     /** 主页面 */
    @RequestMapping("admin/main.html")
    public String main(){
       return  "Wechatarea/WechatareaMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addWechatAreaHtml")
    public String addWechatAreaHtml( ){
             return "Wechatarea/WechatareaAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editWechatAreaHtml")
    public String editWechatAreaHtml(Integer id ,Model model ){
           model.addAttribute("wechatArea",wechatAreaService.selectWechatAreaByIdWechatArea(id));
             return "Wechatarea/WechatareaEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showWechatAreaHtml")
    public String showWechatAreaHtml(Integer id ,Model model ){
           model.addAttribute("wechatArea",wechatAreaService.selectWechatAreaByIdWechatArea(id));
             return "Wechatarea/WechatareaShow";
    }


    /** 分页查询WechatArea
     * @param page  页码
     * @param limit  查几条
     * @param wechatArea  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findWechatAreaByPage")
    @ResponseBody
    public ResultPage findWechatAreaByPage(Integer page, Integer limit,WechatArea wechatArea){
             return wechatAreaService.findWechatAreaByPage(page,limit,wechatArea);
    }

    /** 添加WechatArea
     * @param wechatArea  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addWechatArea")
    @ResponseBody
    public ApiResultModel saveWechatArea(WechatArea wechatArea  ){
             return wechatAreaService.addWechatArea(wechatArea);
    }

    /** 编辑WechatArea
     * @param wechatArea  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editWechatArea")
    @ResponseBody
    public ApiResultModel editWechatArea(WechatArea wechatArea  ){
             return wechatAreaService.editWechatArea(wechatArea);
    }



    /** 删除一条WechatArea
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delWechatArea")
    @ResponseBody
    public ApiResultModel delWechatArea( Integer id  ){
             return wechatAreaService.delWechatArea(id);
    }

    /***
     * 区域ID转换名称
     * @param areas 区域id字符串
     * @return 区域名称
     * **/
    @RequestMapping(value = "admin/getAreaName")
    @ResponseBody
    public String getAreaName(String areas){
        if(areas.contains(",") && areas.length()>0){
            String[] areasstr = areas.split(",");
            String areass = "";
            for (String area: areasstr) {
                WechatArea byIdName = wechatAreaService.getById(area);
                areass += byIdName.getAreaName() ;
            }
            return areass;
        }
        return null;
    }


}
